﻿using System.Windows;
using System.Windows.Controls;
using PengSW.XmlImport;
using PengSW.WindowHelper;

namespace PengSW_XmlImportUi
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class ImportRuleUi : UserControl
    {
        public ImportRuleUi()
        {
            InitializeComponent();
        }

        private ImportRule ImportRule => DataContext as ImportRule;

        private void OnAddRecordDefine_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            this.Exec(() => (e.Parameter as TableDefine).RecordDefines.Add(new RecordDefine(e.Parameter as TableDefine)));
        }

        private void OnAddRecordDefine_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = e.Parameter is TableDefine;
        }

        private void OnRemoveRecordDefine_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            this.Exec(() => (e.Parameter as RecordDefine).TableDefine.RecordDefines.Remove(e.Parameter as RecordDefine));
        }

        private void OnRemoveRecordDefine_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = e.Parameter is RecordDefine;
        }

        private void OnAddFieldDefine_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            this.Exec(() => (e.Parameter as RecordDefine).FieldDefines.Add(new FieldDefine(e.Parameter as RecordDefine)));
        }

        private void OnAddFieldDefine_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = e.Parameter is RecordDefine;
        }

        private void OnRemoveFieldDefine_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            this.Exec(() => (e.Parameter as FieldDefine).RecordDefine.FieldDefines.Remove(e.Parameter as FieldDefine));
        }

        private void OnRemoveFieldDefine_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = e.Parameter is FieldDefine;
        }

        private void OnAddReplaceRule_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            this.Exec(() => ImportRule.ReplaceRules.Add(new ReplaceRule(ImportRule)));
        }

        private void OnAddReplaceRule_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = ImportRule != null;
        }

        private void OnRemoveReplaceRule_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            this.Exec(() =>
            {
                if (e.Parameter is ReplaceRule)
                    ImportRule.ReplaceRules.Remove(e.Parameter as ReplaceRule);
                else
                    (e.Parameter as FieldDefine).Replace = null;
            });
        }

        private void OnRemoveReplaceRule_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = ImportRule != null && (e.Parameter is ReplaceRule || e.Parameter is FieldDefine);
        }

        private void OnAddReplacePattern_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            this.Exec(() => (e.Parameter as ReplaceRule).ReplacePatterns.Add(new ReplacePattern(e.Parameter as ReplaceRule)));
        }

        private void OnAddReplacePattern_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = e.Parameter is ReplaceRule;
        }

        private void OnRemoveReplacePattern_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            this.Exec(() => (e.Parameter as ReplacePattern).ReplaceRule.ReplacePatterns.Remove(e.Parameter as ReplacePattern));
        }

        private void OnRemoveReplacePattern_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = e.Parameter is ReplacePattern;
        }

        private void OnClearReplaceRules_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            if (MessageBox.Show("确定要清除所有的替换规则吗？", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation) != MessageBoxResult.OK) return;
            this.Exec(() => ImportRule.ReplaceRules.Clear());
        }

        private void OnClearReplaceRules_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = ImportRule != null && ImportRule.ReplaceRules.Count > 0;
        }

        private void OnClearRecordDefines_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            if (MessageBox.Show("确定要清除所有的记录提取规则吗？", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation) != MessageBoxResult.OK) return;
            this.Exec(() => ImportRule.TableDefine.RecordDefines.Clear());
        }

        private void OnClearRecordDefines_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = ImportRule != null && ImportRule.TableDefine.RecordDefines.Count > 0;
        }

        private void OnClearFieldDefines_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            if (MessageBox.Show("确定要清除所有的字段提取规则吗？", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation) != MessageBoxResult.OK) return;
            this.Exec(() => (e.Parameter as RecordDefine).FieldDefines.Clear());
        }

        private void OnClearFieldDefines_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = e.Parameter is RecordDefine && (e.Parameter as RecordDefine).FieldDefines.Count > 0;
        }

        private void OnClearReplacePatterns_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            if (MessageBox.Show("确定要清除所有的替换吗？", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation) != MessageBoxResult.OK) return;
            this.Exec(() => (e.Parameter as ReplaceRule).ReplacePatterns.Clear());
        }

        private void OnClearReplacePatterns_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = e.Parameter is ReplaceRule && (e.Parameter as ReplaceRule).ReplacePatterns.Count > 0;
        }
    }
}
